package com.wss.controller;

import com.alibaba.fastjson.JSON;
import com.wss.domain.Account;
import com.wss.domain.Message;
import com.wss.domain.Permission;
import com.wss.service.AccountService;
import com.wss.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 权限请求
 */
@Controller
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    /**
     * 查询所有权限
     * @return
     */
    @RequestMapping(value="/findPermissionAll")
    public String findPermissionAll(Model model, HttpServletRequest request, HttpServletResponse response) {
        // 查询所有权限信息
        List<Permission> permList = permissionService.findAll();
        model.addAttribute("permList",permList);
        return "indexAdmin::table_permission";
    }
    /**
     * 添加权限
     * @return
     */
    @RequestMapping(value="/addPermission", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public String addPermission(Permission permission){
        System.out.println("==============进入到/addPermission=============");
        // 返回给前端的信息对象
        Message message = new Message();
        String msg = "";
        // 执行用户添加方法
        boolean addSuccess = permissionService.addPermission(permission);
        if (addSuccess){
            message.setResCode(1);
            message.setResultMsg("添加成功");
        }else{
            message.setResCode(0);
            message.setResultMsg("添加失败");
        }
        msg = JSON.toJSONString(message);
        return msg;
    }

    /**
     * 修改权限
     * @return
     */
    @RequestMapping(value="/updatePermission", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public String updatePermission(Permission permission){
        System.out.println("==============进入到/updatePermission=============");
        Message message = new Message();
        boolean updateSuccess = permissionService.updatePermission(permission);
        if (updateSuccess){
            message.setResCode(1);
            message.setResultMsg("修改成功");
        }else{
            message.setResCode(0);
            message.setResultMsg("修改失败");
        }
        String msg = JSON.toJSONString(message);
        return msg;
    }
    /**
     * 删除权限
     * @return
     */
    @RequestMapping(value="/deletePermission", method= RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public String deletePermission(Permission permission){
        System.out.println("==============进入到/deletePermission=============");
        // 返回给前端的信息对象
        Message message = permissionService.deletePermission(permission.getId());
        String msg = JSON.toJSONString(message);
        return msg;
    }
}
